//
//  ChartsDataModel.swift
//  TimeBoat
//
//  Created by bo wang on 2022/8/28.
//  Copyright © 2022 Time Boat. All rights reserved.
//

import UIKit
import YYModel

enum ChartsDataUtil {
    static var dateFormatter1: DateFormatter = {
        let df = DateFormatter()
        df.dateFormat = "yyyy-MM-dd HH:mm:ss"
        return df
    }()

    static var dateFormatter2: DateFormatter = {
        let df = DateFormatter()
        df.dateFormat = "yyyy-MM-dd"
        return df
    }()

    static func getDateFrom(str: String?) -> Date? {
        guard let str = str else { return nil }
        return dateFromServerDateStr(str) ?? dateFormatter1.date(from: str) ?? dateFormatter2.date(from: str)
    }
}

@objc class ChartsDataUnit: NSObject, YYModel {
    /// 2022-08-26 06:27:36
    @objc var deviceTime: String?
    /// 2022-08-24
    @objc var date: String?
    @objc var imei: String?
    @objc var userUUID: String?
    
    /// 小数时间，精确到分。比如4.5指4时30分
    lazy var hour: Double? = {
        guard let day = ChartsDataUtil.getDateFrom(str: deviceTime) else { return nil }
        let hms = day.hourMinuteSecond
        return Double(hms.0) + Double(hms.1) / 60
    }()
    
    /// 公历数据中周日是1，周一到周六是 2-7，这里需要将周日排到末尾，所以8是周日。
    lazy var weekday: Int? = {
        guard let day = ChartsDataUtil.getDateFrom(str: date) else { return nil }
        return day.weekday == .sunday ? 8 : day.weekday.rawValue
    }()
    
    lazy var day: Int? = {
        guard let day = ChartsDataUtil.getDateFrom(str: date) else { return nil }
        return day.day
    }()
}

@objc class ChartsYearDataUnit: NSObject, YYModel {
    @objc var imei: String?
    @objc var month: Int = 0
    @objc var userUUID: String?
    @objc var year: Int = 0
    
    // 部分接口才有
    @objc var beginDate: String?
    @objc var days: Int = 0
    @objc var endDate: String?
}

@objc class ChartsDataModel: NSObject, YYModel {
    @objc var info: ChartSummaryInfoModel?
}
